//
//  StoryTableViewController.m
//  老司机发车了
//
//  Created by 程成 on 16/8/6.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "StoryTableViewController.h"
#import "StoryModel.h"
#import "StoryTableViewCell.h"
#import "DetailViewController.h"
@interface StoryTableViewController ()
/*数据数组*/
@property (strong, nonatomic )NSMutableArray *dataArray;
/*topicid*/
@property (strong, nonatomic)NSMutableArray *topicidArray;
//上拉次数
@property (assign, nonatomic)NSInteger index;
@end

@implementation StoryTableViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)topicidArray{
    if (!_topicidArray) {
        _topicidArray = [[NSMutableArray alloc] init];
    }
    return _topicidArray;
}

- (void)analyseDataWithIndex:(NSInteger )index{
    [NetWorkRequestManager requestWithType:GET urlString:StoryUrlIndex parDic:nil finish:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
                for (NSDictionary *dic in dictionary[@"reportList"]) {
                    [self.topicidArray addObject:dic[@"topicid"]];
                }
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",StoryUrlOne,self.topicidArray[index],StoryUrlTwo];

        [self.dataArray removeAllObjects];


        [NetWorkRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            for (NSDictionary *dic in dictionary[@"list"]) {
                StoryModel *model = [[StoryModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                for (NSDictionary *dic0 in dic[@"imageList"]) {
                    [model setValuesForKeysWithDictionary:dic0];
                }
                [self.dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            });
            //结束加载
            [self.tableView.mj_footer endRefreshing];
        } finishError:^(NSError *error) {
            NSLog(@"数据解析有误");
        }];
        //结束加载
        [self.tableView.mj_footer endRefreshing];
    } finishError:^(NSError *error) {
        NSLog(@"解析出错");
    }];
}
//下拉刷新
- (void)pullDownHeader{
    //开始下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index += 1;
        [self analyseDataWithIndex:self.index];
    }];
    //开始上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.index += 1;
        [self analyseDataWithIndex:self.index];
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 0;
    //拉动刷新
    [self pullDownHeader];

    [self analyseDataWithIndex:0];
    [self.tableView registerNib:[UINib nibWithNibName:@"StoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"sCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sCell"];
    if (!cell) {
        cell = [[StoryTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"sCell"];
    }
    StoryModel *model = _dataArray[indexPath.row];
    [cell setupValueWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StoryModel *model = _dataArray[indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.docid = model.docid;
    [self presentViewController:detailVC animated:YES completion:nil];
}

@end
